#include <windows.h>
#include <stdio.h>




int loadimg(char *filepath,BYTE* *imgdata)
{
    BITMAPINFOHEADER bmih;
    BITMAPFILEHEADER bmfh;
    FILE *fp;
    int n;
    int width;
    int height;
    int bitCount;
    long biSizeImage;

    int dwLineBytes;
    
    
    if(!(fp=fopen(filepath,"rb"))){
        printf("Can not open file.\n");
        return -1;
    }
        
    fread(&bmfh,sizeof(BITMAPFILEHEADER),1,fp);
    
    
    fread(&bmih,sizeof(BITMAPINFOHEADER),1,fp);
    
    width=bmih.biWidth;
    height=bmih.biHeight;
    bitCount=bmih.biBitCount;
    biSizeImage=bmih.biSizeImage;


    dwLineBytes=(width*bitCount+31)/32*4;
    
    if(bitCount==24){
        fseek(fp,bmfh.bfOffBits,SEEK_SET);
    }else{
        fclose(fp);/*error */
        return -1;
    }
    
       
    
    fread(*imgdata,dwLineBytes*height*sizeof(BYTE),1,fp);

    fclose(fp);
    
}

void img2str(BYTE* imgdata,char* str)
{
    int r,g,b;
    int height=26;
    int width=80;
    int i,j;
    
    int dwLineBytes=(width*24+31)/32*4;
    
    for(i=0;i<height;i++){
        for(j=0;j<240;){
            
            b=*(imgdata+dwLineBytes*(height-1-i)+j);
            ++j;
            
            g=*(imgdata+dwLineBytes*(height-1-i)+j);
            ++j;
            
            r=*(imgdata+dwLineBytes*(height-1-i)+j);
            ++j;
            
            
            /*one pixel loaded*/
        
            if(r||g||b){
                /*not black*/
                str[i*80+(j/3)-1]=' ';
            }else{
                str[i*80+(j/3)-1]='*';
            }   
            
        }
                     
    }
    str[80*25]='\0';
    
}
    
int main(){
    BYTE* imgdata;
    int i;
    int r,g,b;
    int height=26;
    int width=80;
    char str[80*25+1];
    char filename[50];
    int dwLineBytes=(width*24+31)/32*4;


    imgdata=(BYTE*)malloc(6240*sizeof(BYTE));
    
    for(i=2738;i<=9305;++i){
        wsprintf(filename,"ba%d.bmp",i);
        loadimg(filename,&imgdata);
    
        img2str(imgdata,str);
    
        printf("%s",str);
    }    
    free(imgdata);
    getch();
    
}    
